********************************************************************************
** OWWA-online analysis file

* Version history
* 13.5.24 Vojta

capture log close
log using "$logpath/04_analysis", replace

***********************************************************************
* Table 6: Results from the online dictator game
***********************************************************************
{
use "$dropbox/Analysis/1_clean_data/HK/merged_data_cn.dta", clear
append using "$dropbox/Analysis/1_clean_data/KSA/merged_data_ar.dta", force

local treat1 "P1 G1 PG1"

tabulate match_id, generate(match_id)
tabulate hardwaretype, generate(hardwaretype)

local covars "age_25_29 age_30_34 age_35_39 age_40_44 age_45_49 age_50_54 age_55_59 age_60_64 age_65_above education_primary_complete education_college_incomplete education_college_grad education_post_secondary education_bachelor_above employment_parttime employment_selfemployed employment_unemployed employment_parent employment_student employment_retired income marital_married marital_divorce_widow marital_other children_one children_two children_three children_four children_fiveplus HK KSA match_id2 match_id3 match_id4 match_id5 match_id6 match_id7 match_id8 match_id9 match_id10 hardwaretype2 hardwaretype3"

gen C1_d_eq = C1*d_eq
gen P1_d_eq = P1*d_eq
gen G1_d_eq = G1*d_eq
gen PG1_d_eq = PG1*d_eq
gen C2_d_eq = C2*d_eq
gen P2_d_eq = P2*d_eq
gen G2_d_eq = G2*d_eq
gen PG2_d_eq = PG2*d_eq

gen T1 = 1-C1
gen T1_d_eq = T1*d_eq
gen T2 = 1-C2
gen T2_d_eq = T2*d_eq

* Only use data with both rounds complete (for main analysis)
* Also droping data with incomplete survey data (some people did not want to report their income)
keep if status==4 & income!=.

** 3.3.1 Main research question *************************************************************
gen UAEKSAHK = (UAE | KSA | HK)
gen UAEKSA = (UAE | KSA)

foreach condition in "UAEKSAHK" "HK" "UAEKSA" {
foreach outcome of varlist dg_transfer1 {	
	
	* Multiple hypothesis testing; store p-values for output later
	mhtreg (`outcome' P1 G1 PG1 d_eq `covars' if `condition') (`outcome' G1 P1 PG1 d_eq `covars' if `condition') (`outcome' PG1 P1 G1 d_eq `covars' if `condition'), bootstrap(10000)
	scalar mht_p_tmp = el(r(results),1,6)
	scalar mht_g_tmp = el(r(results),2,6)
	scalar mht_pg_tmp = el(r(results),3,6)

	* Main regression now
	* Needs 2016 estadd version installed: ssc install estout, replace
	reg `outcome' `treat1' d_eq `covars' if `condition', robust
	eststo `outcome'_`condition'_331
	* estimates store dg_transfer1_331
	test PG1 = P1
	estadd scalar eq_pgp = r(p)
	test P1 = G1
	estadd scalar eq_pg = r(p)
	test PG1 = G1
	estadd scalar eq_pgg = r(p)
	sum `outcome' if C1 & `condition'
	scalar cont_mean_tmp = r(mean)
	estadd local mht_p "`: di %3.2f scalar(mht_p_tmp)'"
	estadd local mht_g "`: di %3.2f scalar(mht_g_tmp)'"
	estadd local mht_pg "`: di %3.2f scalar(mht_pg_tmp)'"
	estadd local cont_mean "`: di %3.2f scalar(cont_mean_tmp)'"
	estadd local contvars "Yes"
}
}

* Output 3.3.1 Main research question
esttab dg_transfer1_UAEKSAHK_331 dg_transfer1_HK_331 dg_transfer1_UAEKSA_331, ///
	$cells label keep(P* G*) cells(b(fmt(3)) ci(fmt(2) par)) stats(eq_pg eq_pgp eq_pgg mht_p mht_g mht_pg contvars cont_mean N, fmt(2 2 2 2 2 2 2 0) ///
	labels("pv b_P1=b_G1" "pv b_PG1=b_P1" "pv b_PG1=b_G1" "MHT pv P1" "MHT pv G1" "MHT pv PG1" "Controls" "Control mean" "Observations"))
}

***********************************************************************
* Table E10: Online experiment: Summary statistics, by country and treatment
***********************************************************************
{
use "$dropbox/Analysis/1_clean_data/HK/merged_data_cn.dta", clear
append using "$dropbox/Analysis/1_clean_data/KSA/merged_data_ar.dta", force

* Only use data with both rounds complete (for main analysis)
* Also droping data with incomplete survey data (some people did not want to report their income)
keep if status==4 & income!=.

* generate pooled age variables
gen age1=age_18_24 | age_25_29 | age_30_34
gen age2=age_35_39 | age_40_44 | age_45_49
gen age3=age_50_54 | age_55_59 | age_60_64 | age_65_above

* generate some college dummy
gen some_college= education==4 | education==5 | education==6 

* generate employed dummy
gen employed= employment_status==1 | employment_status==2 | employment_status==3

global balancevars female age1 age2 age3 some_college employed income marital_single marital_married children_dummy 

* Summary statistics: demographics by country *******************************************
estimates clear
estpost sum $balancevars
eststo sum_country_aggregate 
foreach abbrev of varlist HK KSA UAE {
	estpost sum $balancevars if `abbrev'==1
	eststo sum_country_`abbrev'
	}
	
* Summary statistics: demographics by treatment *******************************************
foreach abbrev of varlist C1 P1 G1 PG1 {
	estpost sum $balancevars if `abbrev'==1
	eststo sum_treat_`abbrev'
	}
	
* Summary statistics: demographics by country
esttab sum_country_aggregate sum_country_HK sum_country_KSA sum_country_UAE sum_treat_C1 sum_treat_P1 sum_treat_G1 sum_treat_PG1, cell(mean(fmt(%9.2f)) sd(par("(" ")") fmt(%9.2f))) label
}

***********************************************************************
* Table E11: Online dictator game (robustness checks)
***********************************************************************
{
use "$dropbox/Analysis/1_clean_data/HK/merged_data_cn.dta", clear
append using "$dropbox/Analysis/1_clean_data/KSA/merged_data_ar.dta", force

local treatall "CC PC GC PGC CP CG CPG"
local treatgroup "PC GC PGC CP CG CPG"
local treat1 "P1 G1 PG1"
local treat1C "PC GC PGC"

tabulate match_id, generate(match_id)
tabulate hardwaretype, generate(hardwaretype)

local covars "age_25_29 age_30_34 age_35_39 age_40_44 age_45_49 age_50_54 age_55_59 age_60_64 age_65_above education_primary_complete education_college_incomplete education_college_grad education_post_secondary education_bachelor_above employment_parttime employment_selfemployed employment_unemployed employment_parent employment_student employment_retired income marital_married marital_divorce_widow marital_other children_one children_two children_three children_four children_fiveplus HK KSA match_id2 match_id3 match_id4 match_id5 match_id6 match_id7 match_id8 match_id9 match_id10 hardwaretype2 hardwaretype3"

gen C1_d_eq = C1*d_eq
gen P1_d_eq = P1*d_eq
gen G1_d_eq = G1*d_eq
gen PG1_d_eq = PG1*d_eq
gen C2_d_eq = C2*d_eq
gen P2_d_eq = P2*d_eq
gen G2_d_eq = G2*d_eq
gen PG2_d_eq = PG2*d_eq

gen T1 = 1-C1
gen T1_d_eq = T1*d_eq
gen T2 = 1-C2
gen T2_d_eq = T2*d_eq

* Only use data with at least round 1 complete (for attrition checks)
keep if status!=1

* Only use data with both rounds complete (for main analysis)
* Also droping data with incomplete survey data (some people did not want to report their income)
keep if status==4 & income!=.


** 3.3.1 Main research question *************************************************************
gen UAEKSAHK = (UAE | KSA | HK)
gen COMPSURVEY=(gender!=. & female!=. & age!=. & age_18_24!=. & age_25_29!=. & age_30_34!=. & age_35_39!=. & age_40_44!=. & age_45_49!=. & age_50_54!=. & age_55_59!=. & age_60_64!=. & age_65_above!=. & education!=. & education_primary_below!=. & education_primary_complete!=. & education_college_incomplete!=. & education_college_grad!=. & education_post_secondary!=. & education_bachelor_above!=. & employment_status!=. & employment_fulltime!=. & employment_parttime!=. & employment_selfemployed!=. & employment_unemployed!=. & employment_parent!=. & employment_student!=. & employment_retired!=. & income!=. & marital_status!=. & marital_single!=. & marital_married!=. & marital_divorce_widow!=. & marital_other!=. & children!=. & children_none!=. & children_one!=. & children_two!=. & children_three!=. & children_four!=. & children_fiveplus!=. & children_dummy!=. & country!="" &  q1_1!=. & q1_2!=. & q1_3a!=. & q1_3b!=. & q1_3c!=. & q1_3d!=. & q1_4a!=. & q1_4b!=. & q1_4c!=. & q1_5!="" & q1_6!=. & q1_7!=. & q1_8!=. & match_real!=. & q2_1!=. & q2_2!="" & q2_3!="" & filipino_statements!=. & q3_1_payontime!=. & q3_1_mobileaway!=. & q3_1_reducesalary!=. & q3_1_slap!=. & q3_1_trust!=. & q3_1_respect!=. & q3_1_leavehouse!=. & q3_1_nobreak!=. & q3_2!=. & q3_3!=.)
gen MATCHREAL=match_real==1
gen DEQ = d_eq
gen NDEQ = !d_eq
foreach outcome of varlist dg_transfer1 {	
	
foreach condition in "UAEKSAHK" {
	* Main regression now
	* Needs 2016 estadd version installed: ssc install estout, replace
	reg `outcome' `treat1' d_eq `covars' if `condition', robust
	eststo `outcome'_`condition'_331
	* estimates store dg_transfer1_331
	sum `outcome' if C1 & `condition'
	scalar cont_mean_tmp = r(mean)
	estadd local cont_mean "`: di %3.2f scalar(cont_mean_tmp)'"
	estadd local contvars "Yes"
}
	
* No controls
reg `outcome' `treat1' d_eq , robust
	eststo `outcome'_NOCONTROL_331
	sum `outcome' if C1
	scalar cont_mean_tmp = r(mean)
	estadd local cont_mean "`: di %3.2f scalar(cont_mean_tmp)'"
	estadd local contvars "No"
* Three treatments pooled into a single treatment category
reg dg_transfer1 T1 d_eq, robust
	eststo `outcome'_POOLED_331
	sum `outcome' if C1
	scalar cont_mean_tmp = r(mean)
	estadd local cont_mean "`: di %3.2f scalar(cont_mean_tmp)'"
	estadd local contvars "Yes"
	
foreach condition in "COMPSURVEY" "MATCHREAL" "DEQ" "NDEQ" {
	* Main regression now
	* Needs 2016 estadd version installed: ssc install estout, replace
	reg `outcome' `treat1' d_eq `covars' if `condition', robust
	eststo `outcome'_`condition'_331
	* estimates store dg_transfer1_331
	sum `outcome' if C1 & `condition'
	scalar cont_mean_tmp = r(mean)
	estadd local cont_mean "`: di %3.2f scalar(cont_mean_tmp)'"
	estadd local contvars "Yes"
}
}

* Output 3.3.1 Main research question
esttab dg_transfer1_UAEKSAHK_331 dg_transfer1_NOCONTROL_331 dg_transfer1_POOLED_331 dg_transfer1_COMPSURVEY_331 dg_transfer1_MATCHREAL_331 dg_transfer1_DEQ_331 dg_transfer1_NDEQ_331, ///
	$cells label keep(P* G* T1  d_eq) cells(b(fmt(3)) ci(fmt(2) par)) stats(contvars cont_mean N, fmt( 2 2 0) ///
	labels("Controls" "Control mean" "Observations"))

}

log close
